package app.stock.client;

import java.awt.Component;

import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import app.stock.common.Constants;

public class DataTable extends JTable {
	Object[] tableHeaderData;
	Object[] realTableHeaderData;
	Object[][] tableData;
	Object[][] realTableData;
	private static int additionalCol = 4;

	public DataTable() {
		super();
	}

	public void initialize() {
		if (this.tableHeaderData != null && this.tableHeaderData.length > 0) {
			realTableHeaderData = new Object[tableHeaderData.length + additionalCol];
			realTableHeaderData[0] = Constants.label_sequence;
			realTableHeaderData[1] = Constants.label_operationSelect;
			System.arraycopy(tableHeaderData, 0, realTableHeaderData, 2, tableHeaderData.length);
			realTableHeaderData[tableHeaderData.length + 2] = Constants.label_operationEdit;
			realTableHeaderData[tableHeaderData.length + 3] = Constants.label_operationDelete;
		}
		if (this.tableData != null && this.tableData.length > 0) {
			realTableData = new Object[tableData.length][tableData[0].length + additionalCol];
			for (int i = 0; i < tableData.length; i++) {
				realTableData[i][0] = i + 1;
				realTableData[i][1] = false;
				System.arraycopy(tableData[i], 0, realTableData[i], 2, tableData[i].length);
				realTableData[i][tableData[i].length + 2] = new JButton(Constants.label_operationEdit);
				realTableData[i][tableData[i].length + 3] = new JButton(Constants.label_operationDelete);
			}

		}
		TableModel jTable1Model = new DataTableModel(realTableData, realTableHeaderData);
		TableRowSorter jTable1RowSorter1 = new TableRowSorter();
		jTable1RowSorter1.setModel(jTable1Model);
		this.setModel(jTable1Model);
		this.setRowSorter(jTable1RowSorter1);
		if (this.tableData != null && this.tableData.length > 0) {
			// CheckBox
			this.getColumnModel().getColumn(1).setCellEditor(this.getDefaultEditor(Boolean.class));
			this.getColumnModel().getColumn(1).setCellRenderer(this.getDefaultRenderer(Boolean.class));
			// ID
			this.getTableHeader().getColumnModel().getColumn(2).setMaxWidth(0);
			this.getTableHeader().getColumnModel().getColumn(2).setMinWidth(0);
			this.getColumnModel().getColumn(2).setMaxWidth(0);
			this.getColumnModel().getColumn(2).setMinWidth(0);
			// Update button
			this.getColumnModel().getColumn(tableData[0].length + 2).setCellRenderer(new ButtonTableCellRenderer());
			// Delete button
			this.getColumnModel().getColumn(tableData[0].length + 3).setCellRenderer(new ButtonTableCellRenderer());
		}
	}

	public void setTableData(Object[] tableHeaderData, Object[][] tableData) {
		this.tableHeaderData = tableHeaderData;
		this.tableData = tableData;
	}

	class DataTableModel extends DefaultTableModel {

		public DataTableModel(Object[][] realTableData, Object[] realTableHeaderData) {
			super(realTableData, realTableHeaderData);
		}

		public boolean isCellEditable(int row, int column) {
			if (column == 0 || column == tableHeaderData.length + 2 || column == tableHeaderData.length + 3) {
				return false;
			}
			return true;
		}
	}

	class ButtonTableCellRenderer extends DefaultTableCellRenderer {
		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
				boolean hasFocus, int row, int column) {
			return (JButton) value;
		}
	}

} // @jve:decl-index=0:visual-constraint="28,-3"
